<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - yahoo-auctions</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-yahoo-auctions.html">here</a>.</p>

<div id="yahoo-auctions" style="display: none;">
<b>${query}</b>で Yahoo! Auctions を検索: ${resultsnum}件
    <div class="items" id="items">
        {for item in items}
            <div class="item" style="clear: both;border:1px solid #777777; -moz-border-radius:4px;background:#333333;margin:2px 0px 0px 0px;padding:0px;">
                <div style="border:1px solid #777777; -moz-border-radius:4px;background:#666666;margin:0px;padding:0px;font-size:90%;">
                    <a href="${item.url}" accesskey="${item.key}">
                        <span style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 0px 2px; vertical-align:center; margin-left:1px; max-width:20px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#333333;">${item.key}</span><b>${item.title}</b>
                    </a>
                </div>
                <div class="itemview">
                    <div style="float:left;margin:1px 1px 1px 1px;" id="${item.auctionID}" class="thumbnail" imgsrc="${item.img}">
                        
                    </div>
                    <div style="font-size:75%;" class="sub">
                        現在の価格:<b>${item.price}</b> 入札件数:${item.bids} 終了日時:${item.endtime}{if item.bidorbuy} 希望落札価格:${item.bidorbuy}{/if}
                        出品者:<a href="${item.seller.itemlist}">${item.seller.id}</a>(<a href="${item.seller.rating}">評価</a>)
                        <span class="option">
                            {for opt in item.option}
                                <img src="${opt}" height="12" />
                            {/for}
                        </span>
                        <span style="margin:0px 0px 0px 0px;" id="desc_${item.auctionID}" class="description">
                          <i>説明を取得中...</i>
                        </span>
                    </div>
                </div>
                <div style="clear:both;"> </div>
            </div>
        {forelse}
            <b>該当項目が見つかりませんでした</b>
        {/for}
    </div>
    <div class="bottom" style="position:absolute;bottom:11px;right:0px;height:13px;width:100%;padding:0px;margin:0px;background:rgba(40,40,40,0.7);">
        <div class="note" style="font-size:70%;clear:both;float:left;">※[Ctrl]+[Alt]+[番号] でジャンプ</div>
        <div class="navigation" style="font-size:70%;float:right;">
            <a id="prev" accesskey="8"><span style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 1px 2px; max-width:30px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#000000;box-shadow:2px 2px 2px black;">&lt;Prev(<u>8</u>)</span></a>
            Page <span id="page">${page}</span>
            <a id="next" accesskey="9"><span style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 1px 2px; max-width:30px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#000000;box-shadow:2px 2px 2px black;">Next(<u>9</u>)&gt;</span></a>
        </div>
    </div>
</div>


<script class="commands"><![CDATA[

CmdUtils.CreateCommand({
    name: "yahoo-auctions",
    description: "Yahoo! Auctions",
    takes: {search: noun_arb_text},
    icon: "http://auctions.yahoo.co.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",
    
    preview: function( previewBlock, directObject ) {
        
        var ubiquityVersion = Application.prefs.get("extensions.ubiquity.lastversion").value;
        
        var yahooAppID = "XdZkt_2xg662prW1szQaDNcDSWr8YgQDYMWcQeJ7STR4QQirEK1fGlkUbAgoqKKFZxhA7g--";
        var apiurl = "http://auctions.yahooapis.jp/AuctionWebService/V1/Search";
        
        
        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 1) {
            var previewStr = "Yahoo! Auctions を検索します";
            previewBlock.innerHTML = previewStr;
            return;
        }
        
        var previewTemplate = "<b>${query}</b> を Yahoo! Auctions で検索します...<div id='items'></div>";
        var previewData = {query: searchText};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        
        renderItems(0);
        
        function renderItems(pageDiff){
            
            jQuery("#items", previewBlock).replaceWith('<div style="position:absolute;top:40%;left:40%;width:100px;height:50px;" id="progressimage"><img src="http://ubiquity-commands-ja-nora.googlecode.com/files/image_7990.gif" /></div>').find("#progressimage").hide().fadeIn(2000);
            
            var page = eval(jQuery("#page" ,previewBlock).text());
            if(page > 0){
                page = page + pageDiff;
            }else{
                page = 1;
            }
            
            var apiPage = Math.floor( ( page - 1 ) * 5 / 50 ) + 1;
            var apiItemIndex = ( (page - 1) * 5 ) - ( apiPage - 1 ) * 50;
            
            CmdUtils.previewAjax(previewBlock, {
                type: "GET",
                url: apiurl,
                data: {appid: yahooAppID, query: searchText, page: apiPage},
                dataType: "xml",
                
                error: function() {
                    previewBlock.innerHTML = "Error searching Yahoo! Auctions.";
                },
                
                success: function(responseData) {
                
                    responseData = jQuery(responseData);
                    var items = [];
                    var i = 0;
                    
                    var resultsnum = responseData.find("AuctionSearchResult").attr("totalAuctions");
                    
                    responseData.find("item").slice(apiItemIndex, apiItemIndex + 5).each(function(){
                        
                        var resultList = jQuery(this);
                        
                        var auctionID = resultList.find("auctionID").text();
                        var title = resultList.find("title").text();
                        var url = resultList.find("url").text();
                        var img = resultList.find("img").text();
                        var price = resultList.find("price").text();
                        var bids = resultList.find("bids").text();
                        var endtime = resultList.find("endtime").text().replace(/ /g,"");
                        var bidorbuy = resultList.find("bidorbuy").text();
                        
                        var seller = [];
                        seller.id = resultList.find("id").text();
                        //seller.itemlist = resultList.find("itemlist").text();
                        seller.itemlist = "http://openuser.auctions.yahoo.co.jp/jp/user/" + seller.id;
                        seller.rating = resultList.find("rating").text();
                        
                        var option = [];
                        resultList.find("option").children().each(function(){
                            option.push(jQuery(this).text());
                        });
                        
                        var key = ++i;
                        
                        var item = {key : key,
                                    auctionID : auctionID,
                                    title : title,
                                    url : url,
                                    img : img,
                                    price : price,
                                    bids : bids,
                                    endtime : endtime,
                                    bidorbuy : bidorbuy,
                                    seller : seller,
                                    option : option
                                    };
                        
                        items.push(item);
                        
                    });
                    
                    
                    var previewData = {
                        query : searchText,
                        resultsnum : resultsnum,
                        items : items,
                        page : page
                    };
                    
                    
                    previewBlock.innerHTML = CmdUtils.renderTemplate(
                        jQuery("#yahoo-auctions", feed.dom).html(),
                        previewData);
                    
                    
                    
                    if(page>1){
                        jQuery("#prev", previewBlock).show();
                        jQuery("#prev", previewBlock).click(function(){
                            renderItems( -1 );
                            jQuery(this).unbind();
                        });
                    }else{
                        jQuery("#prev", previewBlock).hide();
                    }
                    
                    if(resultsnum > page * 5){
                        jQuery("#next", previewBlock).show();
                        jQuery("#next", previewBlock).click(function(){
                            renderItems( 1 );
                            jQuery(this).unbind();
                        });
                    }else{
                        jQuery("#next", previewBlock).hide();
                    }
                    
                    
                    //for Ubiquity 0.1.x / 0.5  difference
                    if (ubiquityVersion.indexOf("0.1.") > -1){
                        jQuery("#prev", previewBlock).attr({href:""});
                        jQuery("#next", previewBlock).attr({href:""});
                    }else{
                        jQuery("#prev", previewBlock).removeAttr("href");
                        jQuery("#next", previewBlock).removeAttr("href");
                    }
                    
                    
                    jQuery(previewBlock).find(".itemview").each( function(){
                        
                        var id = jQuery(this).find(".thumbnail").attr("id");
                        fetchYahooAuctionsItem(this, id);
                        
                    });
                    
                }
            });
        
        
        }

    },

    execute: function( directObject ) {
        Utils.openUrlInBrowser( "http://search.auctions.yahoo.co.jp/jp/search/auc?p=" + escape( Utils.convertFromUnicode("euc-jp", directObject.text) ) );
    }
});

function fetchYahooAuctionsItem(previewBlock, id){
    
    var yahooAppID = "XdZkt_2xg662prW1szQaDNcDSWr8YgQDYMWcQeJ7STR4QQirEK1fGlkUbAgoqKKFZxhA7g--";
    var apiurl = "http://auctions.yahooapis.jp/AuctionWebService/V1/AuctionItem";
    var param = {  appid : yahooAppID, auctionID : id};
    
    
    CmdUtils.previewAjax(previewBlock, { 
        type: "GET",
        url: apiurl,
        data: param,
        datatype: "XML",
        error: function(){
            jQuery(previewBlock).find("div.description").html("<i>failed</i>");
        },
        success: function(responseData) {
            responseData = jQuery(responseData);
            
            var desc = responseData.find("description").text();
            desc = desc.replace( /<[\s\S]*?>/g, "").replace(/\s+/g, " ");
            desc = desc.substring(0, 100) + "...";
            
            var timeleft = responseData.find("timeleft").text();
            
            jQuery(previewBlock).find("span.description").text(desc);
            jQuery(previewBlock).find("span.option").before("残り:" + timeleft + " ");
            
            var imgTemplate = '<img src="${hs}" height="${hh}" />';
            var imgData = {hs:jQuery(previewBlock).find(".thumbnail").attr("imgsrc") , hh:40 };
            
            jQuery(previewBlock).find("div.thumbnail").html(CmdUtils.renderTemplate(imgTemplate, imgData));
            
        }
    });
    
    
}


]]></script>
</body>
</html>
